package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import java.util.List;
import java.util.UUID;

@Service
//@Transactional
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private IUserInfoRedisService redisService;

    @Autowired
    private UserInfoRepository repository;

    @Override
    public void save(UserInfo userInfo) {
        repository.save(userInfo);

    }

    @Override
    public void update(UserInfo userInfo) {
        repository.save(userInfo);

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return repository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {

        UserInfo userInfo = repository.findByPhone(phone);
        return userInfo != null;
    }

    @Override
    public void sendVerifyCode(String phone) {

        //创建验证码
        String code = UUID.randomUUID().toString()
                .replaceAll("-", "")
                .substring(0,4);
        //创建短信
        StringBuilder sb = new StringBuilder(80);
        sb.append("【嘿咻】注册的短信验证码为:").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟内使用");

        System.out.println(sb);

        /*//真实发送短信,本质是使用java发起http请求
        //Springmvc提供RestTemplate用于发起http请求
        RestTemplate template = new RestTemplate();
        //参数1 短信接口url
        String url = "https://way.jd.com/chuangxin/dxjk?mobile={1}&content={2}&appkey={3}";
        //参数2 请求接口完之后相应数据封装对象类型
        //参数3 请求参数列表
        String ret = template.getForObject(url, String.class, phone, sb.toString(), "b307f9ed8117cc38c3cd6d5397810c5a");
        System.out.println(ret);
        if (!ret.contains("Success")){
            throw new RuntimeException("短信发送失败");
        }*/


        //把验证码保存进redis
        redisService.sendVerifyCode(phone, code);

    }

    @Override
    public void regist(String nickname, String password, String rpassword, String phone, String verifyCode) {
        //校验参数是否为空
        /*if (!StringUtils.hasLength(phone)){
            throw new RuntimeException("手机号码不能为空");
        }*/
        //使用断言工具类
        AssertUtil.hasLength(nickname, "用户名不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(phone, "手机号码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        //校验2次输入密码是否相同
        AssertUtil.isEquals(password,rpassword,"两次输入密码不相同");

        //校验手机号格式是否正确

        //校验手机号是否唯一
        if (this.checkPhone(phone)){
            throw new LogicException("手机号码已经被注册");
        }

        //校验短信验证码是否正确
        String code = redisService.getVerifyCode(phone);
        if (!verifyCode.equals(code)){
            throw new LogicException("验证码失效或者错误");
        }

        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setEmail("");
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setInfo("");
        userInfo.setGender(UserInfo.GENDER_SECRET);
        userInfo.setHeadImgUrl("/images/default.jpg");
        //state是核心属性,必须自己控制
        userInfo.setState(UserInfo.STATE_NORMAL);

        this.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfo userInfo = repository.findByPhone(username);
        if (userInfo==null || !userInfo.getPassword().equals(password)){
            throw new LogicException("账号密码错误");
        }

        //给密码设置为空,让页面不显示密码
        userInfo.setPassword("");
        return userInfo;
    }

    @Override
    public List<UserInfo> queryByCity(String city) {
        return repository.findByCity(city);
    }
}
